package com.atguigu.gulimall.search.Thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static   ExecutorService service = Executors.newFixedThreadPool(1);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main---start");
//        CompletableFuture.runAsync(()->{
//            System.out.println("Thread1: "+Thread.currentThread().getId());
//            int i=10/2;
//            System.out.println(i);
//        },service);

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("Thread1: "+Thread.currentThread().getId());
//            int i=10/2;
//            System.out.println(i);
//            return  i;
//        }, service).whenComplete((result,exception)->{
//            System.out.println("异步任务完成了,结果是"+result+",异常是"+exception);
//                }).exceptionally(throwable->{
//                    return 10;
//        });
//        Integer integer = future.get();

        /**
         * thenRunAsync 不能获取到上一步的执行结果
         * thenAcceptAsync  能接收上一步结果，无返回值
         * thenApplyAsync  能接收上一步结果，有返回值
         */

//           .thenRunAsync(()->{
//           System.out.println("任务2启动咯");
//       },service);
//        .thenAcceptAsync(res->{
//            System.out.println("任务2启动，上一步返回结果+"+res);
//        },service)
//       .thenApplyAsync(res->{
//           System.out.println("任务2启动");
//           return "hello"+res;
//       },service);
//
//        System.out.println("main---end");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一开始: " + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务一结束：" + i);
            return i;
        }, service);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二开始: " + Thread.currentThread().getId());
            System.out.println("任务二结束：");
            return "hello";
        }, service);
//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务三开始");
//        },service);
       /* future01.thenAcceptBothAsync(future02,(f1,f2)->{
            System.out.println("任务三开始...之前的结果"+f1+"---"+f2);
        },service); */
        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
            return f1 + " : " + f2 + "->haha";
        }, service);
        System.out.println("main---end"+future.get());


//        Thread1 thread1 = new Thread1();
//        thread1.start();

   /*     FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
        new Thread(futureTask).start();
        Integer integer = futureTask.get();
        System.out.println("返回的结果是:"+integer);*/
//        service.execute(new Thread1());


//        System.out.println("main end");
    }

    public static class  Thread1 extends Thread{
        @Override
        public void run() {
            System.out.println("Thread1: "+Thread.currentThread().getId());
            int i=10/2;
            System.out.println(i);
        }
    }

    public static class  Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程: "+Thread.currentThread().getId());
            int i=10/2;
            System.out.println(i);
            return i;
        }
    }
}
